// pages/discoverList/discoverList.js
let app = getApp()
Page({
  onShareAppMessage(options) {
    let img = this.data.lists.length > 0 ? this.data.lists[0].listImg : ''
    let url = '/pages/discoverList/discoverList'
    let imgUrl = ''
    if (this.data.isNavBack) {
      let selectedTopicId = wx.getStorageSync('selectedTopicId')
      url = '/pages/discoverList/discoverList?isShare=1&topicId=' + selectedTopicId
    }
    let title = '悦龄会|打造熟年品质生活'
    if (this.data.isNavBack || this.data.isShare) {
      title = this.data.selectedItem.title
      imgUrl = this.data.selectedItem.listImg
    }
    return {
      title: title,
      path: url,
      imageUrl : imgUrl,
      success: function (res) {
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    footeractive: 1,
    lists: [],
    pageNo: 1,
    pageSize: 5,
    totalPage: 2,
    largeKey: 0,
    cheight: 0,
    isscroll: true,
    selectedItem: {}, // 如果用户从分享进入就会在第一个放大显示点击进入详情的那个产品
    isShare: false, //用户是否从分享入口进入
    showIcon: false
  },
  bindtap (e) {
    let id = e.currentTarget.dataset.topicid
    let selectItem = e.currentTarget.dataset.item
    let idx = e.currentTarget.dataset.id
    wx.setStorageSync('selectedTopicId', id)
    wx.setStorageSync('selectedpageNo', this.data.pageNo)
    wx.setStorageSync('selectedItem', Object.assign(selectItem, {idx: idx}))
    let url = app.globalData.frontUrl + 'discoverArticleDetail?topicId=' + id + '&pageNo=' + this.data.pageNo
    if (selectItem.tabId === 6) {
      let pro = selectItem.tps[0]
      if (pro.productGroupType === 1) {
        url = app.globalData.frontUrl + 'fun/detail?frommini=1&id=' + pro.pid + '&skuId=' + pro.skuId + '&topicId=' + id
         
      }
      if (pro.productGroupType === 2) {
        url = app.globalData.frontUrl + 'goods/detail?frommini=1&pid=' + pro.pid + '&skuId=' + pro.skuId + '&topicId=' + id
      }
      if (pro.productGroupType === 3) {
        url = app.globalData.frontUrl + 'goods/travelDetail?frommini=1&pid=' + pro.pid + '&catId=' + pro.catId + '&skuId=' + pro.skuId + '&topicId=' + id
      }
    }
    wx.navigateTo({
      url: '/pages/middlewave/middlewave?href=' + encodeURIComponent(url),
    })
  },
  getList (pageNo = null) {
    wx.showLoading()
    pageNo = pageNo || this.data.pageNo
    wx.request({
      url: app.globalData.baseUrl + '/tab/getTabTopicList',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        tabId: '4,6',
        pageNo: pageNo,
        pageSize: this.data.pageSize },
        success: response => {
          wx.hideLoading()
          let body = response.data
          if (body.code === 0) {
            let lists = body.data
            let data = lists.pageDate
            this.setData({
              totalPage: lists.pageTotal,
              lists: this.data.lists.concat(data),
              isscroll: true
            })
            if (this.data.isShare && !this.data.showIcon) {
              // 用户从分享入口进来的时候，获取分享的item放大显示
              this.getLargerItem()
            }
            if (this.data.isShare && this.data.showIcon) {
              // 用户从下单成功页面回来
              this.setData({
                selectedItem: wx.getStorageSync('selectedItem')
              })
            }
            if (!this.data.isBack && this.data.pageNo === 1 && !this.data.isShare) {
              this.setData({
                largeKey: this.data.lists[0].topicId
              })
            }
          }
          else {
            wx.showModal({
              title: '提示',
              content: body.msg,
            })
          }
      },
      fail: res => {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '获取失败，请稍后再试',
        })
      }
    })
  },
  getLargerItem () {
    wx.request({
      url: app.globalData.baseUrl + '/topic/getTopicAndProduct',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {topicId: this.shareTopicId},
      success: (res) => {
        this.setData({
          selectedItem: res.data.data
        })
      }
    })
  },
  bindscrolltolower () {
    if ((this.data.totalPage > this.data.pageNo) && this.data.isscroll) {
      this.setData({
        isscroll: false
      })
      wx.showLoading()
      let p = this.data.pageNo + 1
      this.setData({
        pageNo: p
      })
      this.getList()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      isShare: options.isShare,
      showIcon: options.showIcon
    })

    var _self = this
    wx.getSystemInfo({
      success: function (res) {
        _self.setData({
          cheight: res.windowHeight - 44
        })
      }
    })
    this.shareTopicId = options.topicId // 这个是通过列表页面本身分享或者详情页分享过来的topicId,用在产品放大显示上
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 判断用户是否是返回到的本页面
    if (this.data.isNavBack) {
      // 用户从返回入口进来的时候，需要定位到所点击产品的页数和再该页数里面产品放大，并且上拉下拉加载页码设置
      this.setData({
        largeKey: wx.getStorageSync('selectedTopicId'),
        selectedItem: wx.getStorageSync('selectedItem'),
        pageNo: wx.getStorageSync('selectedpageNo') || 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // this.setData({
    //   isShare: false,
    //   showIcon: false
    // })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  }
})